package com.uchengwang.ui.cm_ui.cycle.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.adapter.CycleThemesAdapter;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleThemeBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2018/5/26.
 * 获取搜索的社圈主题
 */

public class CycleThemesActivity extends BaseWithTitleActivity{
    private RecyclerView recycleView;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private CycleThemesAdapter adapter;
    private int page =1, totlaPagers;

    private CycleThemeBean cycleBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ustudy_2);
        setRightText("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<CycleThemeBean> data = adapter.getData();
                for (CycleThemeBean bean : data){
                    if (bean.isCheck()){
                        cycleBean = bean;
                        break;
                    }
                }

                if (cycleBean == null){
                    UCommonUtil.getUtil().showToast(CycleThemesActivity.this, "请选择主题");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra(BaseConfig.BEAN, cycleBean);
                setResult(1, intent);
                CycleThemesActivity.this.finish();
            }
        });

        initView();
    }

    private void initView(){
        recycleView = (RecyclerView) findViewById(R.id.u_study_recycleView);
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.mRefreshLayout);

        initData();

    }

    private void initData(){
        adapter = new CycleThemesAdapter(this);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(mAdapter);

        mSwipeRefreshLayout.setAutoLoadMoreEnable(true);
        mSwipeRefreshLayout.setLoadMoreEnable(true);

        mSwipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                mSwipeRefreshLayout.loadMoreComplete(true);
                getThemes(true);

            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    getThemes(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });
    }

    //获取社圈主题
    private void getThemes(final boolean isRefresh){
        CycleHttpUtils.getUtils().getCycleTheme(this, 0, page, new OnHttpResultListener<CycleThemeBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<CycleThemeBean> list) {
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }

                totlaPagers =total;

                if (list != null){
                    if(isRefresh){
                        adapter.setData(list);
                    }else {
                        adapter.getData().addAll(list);
                        adapter.notifyDataSetChanged();
                    }
                }

            }
        });
    }
}
